package com.tyersoft.kickcalendar.adapters;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.DialogPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;

public class DatePickerPreference extends DialogPreference implements DatePicker.OnDateChangedListener{
    private DatePicker datePicker;
    //private Date pickedDate;
    //private Date today;
    private int _year;
    private int _month;
    private int _day;

    public DatePickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public DatePickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected View onCreateDialogView() {
        this.datePicker = new DatePicker(getContext());
        //setToday();

        readSharedPreferences();

        try {
			datePicker.init(_year, _month, _day, this);
		} catch (Exception e) {
		}
        return datePicker;
    }

    @Override
    public void onDateChanged(DatePicker datePicker, int year, int month, int day) {
    	_year = year;
    	_month = month;
    	_day = day;
    }

    
    @Override
    public void onClick(DialogInterface dialog, int which) {
        super.onClick(dialog, which);
        datePicker.clearFocus();
        onDateChanged(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
        onDialogClosed(which == DialogInterface.BUTTON1);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        if(!positiveResult)
            return;
        saveSharedPreferences();
        super.onDialogClosed(positiveResult);
    }
    
//    private void setToday(){
//        Calendar c = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
//        c.set(Calendar.HOUR_OF_DAY, 0);
//        c.set(Calendar.MINUTE, 0);
//        c.set(Calendar.SECOND, 0);
//        c.set(Calendar.MILLISECOND, 0);
//        today = new Date(c.getTimeInMillis());
//    }

    private void saveSharedPreferences(){
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(getKey(), getDate());
        editor.commit();
    }

    private void readSharedPreferences() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
        String value = preferences.getString(getKey(), "");
        // TODO Calendar  
        _year = 2000;
        _month = 0;
        _day = 1;
    	try {
    		_year = Integer.parseInt( value.substring(0, 4) );
    		_month = Integer.parseInt( value.substring(5, 7) ) - 1;
    		_day = Integer.parseInt( value.substring(8, 10) );
		} catch (Exception e) {
        	
		}
    }
    private String getDate(){
    	return String.format("%04d-%02d-%02d", _year, _month+1, _day);
    	//DecimalFormat formatter = new DecimalFormat(pattern, dfs);
        //return formatter.format("0000",formatter)+formatter.format("00",_month,2)+"-"+formatter.format("00",_day,2);
    }

}
